import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class TestAppstore:
    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.09/appStore/GET_get_chart_all_version.yaml'))
    def test_get_chart_all_version(self, yaml):
        # 查询指定Chart（所有版本）
        uri = yaml['request']['url'].replace("{repo}", datas.test_appstore["repo"])
        uri = uri.replace("{chart}", datas.test_appstore["chart"])
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml',
                             YamlUtil().read_testcase_yaml('v24.09/appStore/GET_get_chart_specified_version.yaml'))
    def test_get_chart_specified_version(self, yaml):
        # 查询指定Chart（指定版本）
        uri = yaml['request']['url'].replace("{repo}", datas.test_appstore["repo"])
        uri = uri.replace("{chart}", datas.test_appstore["chart"])
        uri = uri.replace("{version}", datas.test_appstore["chartVersion"])
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.09/appStore/GET_get_chart_files.yaml'))
    def test_get_chart_files(self, yaml):
        # 查询Chart文件
        uri = yaml['request']['url'].replace("{repo}", datas.test_appstore["repo"])
        uri = uri.replace("{chart}", datas.test_appstore["chart"])
        uri = uri.replace("{version}", datas.test_appstore["chartVersion"])
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml',
                             YamlUtil().read_testcase_yaml('v24.09/appStore/GET_get_localrepo_charts_count.yaml'))
    def test_get_localrepo_charts_count(self, yaml):
        # 获取本地仓库包数量
        uri = yaml['request']['url']
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.09/appStore/GET_get_official_repo_tags.yaml'))
    def test_get_official_repo_tags(self, yaml):
        # 获取官方仓库标签
        uri = yaml['request']['url']
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.09/appStore/GET_get_time_offset.yaml'))
    def test_get_time_offset(self, yaml):
        # 获取服务器所在的UTC时区
        uri = yaml['request']['url']
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']
